<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Order extends Zend_Db_Table{
	
	protected $_name    = "orders";
    protected $_primary = "id";
    
    public function saveData($data, $items){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('orders', $data);
            
            $id = $db->lastInsertId("orders");
            
            foreach($items as $item){
                $item['order_id'] = $id;
                $item['product_id'] = $item['id'];
                
                unset($item['id']);
                
                $db->insert('order_details', $item);
            }
            
            $db->commit();
            
            return $id;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function saveOrderDetail($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('order_details', $data);
            
            $db->lastInsertId("order_details");
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
}